<?php
    if(isset($_POST['type'])){
        $templateID=$_POST['templateID'];
        $type=$_POST['type'];
        model_template($templateID,$type);
    }
    else if(isset($_POST['createNewType'])){
        if($_POST['createNewType']==1)
            createNewType();
        else
            deleteTypemodel();
    }
    else if(isset($_POST['insertUnitType'])){
        $typename=$_POST['typename'];
        insertUnitType($typename);
    }
    else if(isset($_POST['u_type'])){
        $type=$_POST['u_type'];
        $tpl_id=$_POST['id'];
        $tpl_name=$_POST['name'];
        $tpl_dsc=$_POST['dsc'];
        $css=$_POST['css'];
        $wlus=$_POST['wlus'];
        $unittype=$_POST['unittype'];
        changetpl($tpl_id,$tpl_name,$tpl_dsc,$wlus,$unittype,$css);
    }
    else if(isset($_POST['serch_name'])){
        $tpl_name=$_POST['serch_name'];
        if($tpl_name==null){
            showsuperlist();
        }
        else{
            searchtpl($tpl_name);
        }
    }
    else if(isset($_POST['createTemplate'])){
        $tpl_name=$_POST['tpl_name'];
        $sup_tpl=$_POST['sup_tpl'];
        $unittype=$_POST['unittype'];
        $wlu=$_POST['wlu'];
        $tpl_ds=$_POST['tpl_ds'];
        createTemplate($tpl_name,$sup_tpl,$unittype,$wlu,$tpl_ds);
    }
    else if(isset($_POST['deletetype'])){
        $unitid=$_POST['typeid'];
        deletetype($unitid);
    }
    else if(isset($_POST['delete_tpl'])){
        $tplid=$_POST['tplid'];
        deletetemplate($tplid);
    }
    function deletetemplate($tplid){
        include"sqlconnect.php";
        $sql="DELETE FROM template WHERE templateID='$tplid'";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
        echo "success";
    }
    function deletetype($unitid){
        include 'sqlconnect.php';
        $sql="DELETE FROM unittype WHERE UnitID=$unitid ";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
        createTemplateShow();
    }
    function deleteTypemodel(){
        include 'sqlconnect.php';
        $sql_1="SELECT count(1) as num FROM unittype WHERE UnitID not in(SELECT distinct unitType FROM template WHERE unitType is not null)";
        $result=mysqli_query($conn,$sql_1);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        $sql="SELECT * FROM unittype WHERE UnitID not in(SELECT distinct unitType FROM template WHERE unitType is not null)";
        $result=mysqli_query($conn,$sql);
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>               
                        <h4 class='modal-title' id='myModalLabel'> Create New Type</h4>
                    </div>
                    <div class='modal-body'>
                        <form role='form' class='form-horizontal'>                    
                            <div class='form-group'>
                                <label class='col-sm-3 home-label '>Type Name</label>
                                <div class='col-sm-9'>
                                <select id='slt_del_type' class='form-control input-formgroup'>
                                ";
                                if($num==0){
                                    echo"<option value='none'>None</option>";
                                }else{
                                    while($row=mysqli_fetch_array($result)){
                                        $id=$row['UnitID'];
                                        $typename=$row['TypeName'];
                                        echo"<option value='$id'>$typename</option>";
                                    }
                                }
        
                            echo"</select>
                                </div>
                            </div>
                            </form>
                    </div>   
                    <div class='modal-footer'>                   
                        <button id='delete_type' type='button' class='btn btn-primary' data-dismiss='modal'>DELETE</button>
                    </div>    
                </div>
             </div>";
             mysqli_close($conn);
    }
    function insertUnitType($typename){
        include 'sqlconnect.php';
        $sql="SELECT * FROM unittype WHERE TypeName='$typename'";
        $result=mysqli_query($conn,$sql);
        if(mysqli_fetch_array($result)){
            echo'False';
        }else{
            $sql="INSERT INTO unittype(TypeName) values ('$typename')";
            mysqli_query($conn,$sql);
            createTemplateShow();
        }
        mysqli_close($conn);
        
    }
    function createNewType(){
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>               
                        <h4 class='modal-title' id='myModalLabel'>Create New Type</h4>
                    </div>
                    <div class='modal-body'>
                        <form id='crt_unit_model' role='form' class='form-horizontal'>                    
                            <div class='form-group'>
                                <label class='col-sm-3 home-label '>Type Name</label>
                                <div class='col-sm-9'>
                                    <input type='text' id='newtypename' class='form-control'>
                                </div>
                            </div>
                            </form>
                    </div>   
                    <div class='modal-footer'>                   
                        <button id='create_new_type' type='button' class='btn btn-primary' data-dismiss='modal'>Submit</button>
                    </div>    
                </div>
             </div>";
    }
    function createTemplate($tpl_name,$sup_tpl,$unittype,$wlu,$tpl_ds){
        include "sqlconnect.php";
        $sql="SELECT * FROM template WHERE templateName='$tpl_name'";
        $result=mysqli_query($conn,$sql);
        if(mysqli_fetch_array($result)){
            echo"<div class='fail-modify'>The Template Name $tpl_name is exist.</div>";
        }else{
            if($unittype=='none'){
                $sql="INSERT INTO template(templateName,description) 
                        VALUES ('$tpl_name','$tpl_ds')";
                mysqli_query($conn,$sql);
            }else{
                
                $sql="INSERT INTO template(templateName,description,normalWLUs,unitType) 
                        VALUES ('$tpl_name','$tpl_ds',$wlu,$unittype)";
                mysqli_query($conn,$sql);
                $tplid=mysqli_insert_id($conn);
                $sql="INSERT INTO template_hierarchy(templateID,subTemplateID)
                VALUES($sup_tpl,$tplid)";
                mysqli_query($conn,$sql);
                
            }
            
            echo"<div class='success-modify'>Creat successfull!</div>";
            mysqli_close($conn);
        }
    }
    function createTemplateShow(){
        include "sqlconnect.php";
        $sql="SELECT * FROM template WHERE unitType is NULL";
        $result=mysqli_query($conn,$sql);
        $sql_1="SELECT * FROM unittype order by TypeName";
        $result_1=mysqli_query($conn,$sql_1);
        echo"<form role='form'>
            <div class='col-md-4 col-md-offset-1'>
                <label>New Template Name</label>
                <div class='input-group input-formgroup' >
                    <input id='tpl_name' type='text' class='form-control ' required='required'>
                </div>
                <label>Choose a Super Template</label>
                <select id='sup_tpl' class='form-control input-formgroup'>
                <option value='none'>None</option>";
                 while($row=mysqli_fetch_array($result)){
                     $tplid=$row['templateID'];
                     $tplname=$row['templateName'];
                     echo"<option value='$tplid'>$tplname</option>>";
                 }
            echo"</select>
                <label>Unit Type</label>
                <select id='unittype' class='form-control input-formgroup'>
                    <option value='none'>None</option>";
                    while($row=mysqli_fetch_array($result_1)){
                        $unitid=$row['UnitID'];
                        $typename=$row['TypeName'];
                        echo"<option value='$unitid'>$typename</option>";
                    }
            echo"<option value='newtype' style='color: #00FF00;'>Create New Type</option>
                <option value='deltype' style='color: #FF0000;'>Delete Type</option>
                </select>
                <label class='wlu_show' style='display:none'>WLU</label>
                <div class='input-group input-formgroup wlu_show' style='display:none'>
                    <input id='wlu' type='number' class='form-control ' required='required'>
                </div>
                   
            </div>
            <div  class='col-md-4 col-md-offset-2'>
                <label>Template Description:</label>
                <div class='input-group input-formgroup'>
                    <textarea id='tpl_ds' class='form-control' rows='11' placeholder='Description'></textarea>
                </div> 
                
            </div>
            </form>
            ";
    }
    function searchtpl($tpl_name){
        include 'sqlconnect.php';
        $sql="SELECT *FROM template WHERE templateName='$tpl_name'";
        $result=mysqli_query($conn,$sql);
        echo"<div class='container template-list-item template-list-header'>
            <div class='col-md-5'>Template Name</div>       
            <div class='col-md-2 '>View</div>
            <div class='col-md-2 '>Update</div>
            <div class='col-md-2 '>Delete</div>
        </div>";
        while($row=mysqli_fetch_array($result)){
            $templateID=$row['templateID'];
            $templateName=$row['templateName'];
            $levelNum=1;
            $listID="l_".$templateID;
        echo"<div class='container template-list-item template-level-$levelNum $listID' data-css='$levelNum'>
                <div class='col-md-5'>$templateName</div>       
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='V'>View</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='U'>Update</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='D'>Delete</button></div>";
                $t=mysqli_query($conn,'SELECT count(1) AS num FROM template_hierarchy WHERE templateID='.$templateID);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
                
            }
            if($num>0){
            echo"<div class='col-md-1 ' data-id='$templateID'><span class='glyphicon glyphicon-triangle-bottom subtemplete' data-id='$templateID'></span></div>
                </div><div class='$templateID'>";
                    showsublist($templateID,$levelNum);
            echo"</div>";
            
            }
            else{
                echo"</div>";
            }        
        }
    }
    function changetpl($tpl_id,$tpl_name,$tpl_dsc,$wlus,$unittype,$css){
        include 'sqlconnect.php';
        if(mysqli_fetch_array(mysqli_query($conn,"SELECT *FROM template_hierarchy WHERE templateID=$tpl_id"))){
            $sql="UPDATE template SET templateName='$tpl_name',description='$tpl_dsc' WHERE templateID=$tpl_id";
        }
        else{
            $sql="UPDATE template SET templateName='$tpl_name',description='$tpl_dsc',normalWLUs=$wlus,unitType=$unittype 
            WHERE templateID=$tpl_id";
        }
        mysqli_query($conn,$sql);
        echo"   <div class='col-md-5'>$tpl_name</div>       
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$tpl_id' data-id='V'>View</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$tpl_id' data-id='U'>Update</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$tpl_id' data-id='D'>Delete</button></div>";
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM template_hierarchy WHERE templateID='.$tpl_id);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-bottom subtemplete' data-id='$tpl_id'></span><span class='glyphicon glyphicon-ok success-coin'></span></div>";
            }
            else{
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-ok success-coin'></span></div>";
            }
        mysqli_close($conn);
    }
    function model_template($templateID,$type){
         echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>";   
            if($type=='V'){
                echo"<h4 class='modal-title' id='myModalLabel'>View template Information</h4>
                    </div>
                    <div class='modal-body'>";
                templateview($templateID);
                echo"</div>   
                    <div class='modal-footer'>                   
                    <button type='button' class='btn btn-default' data-dismiss='modal'>CLOSE</button>
                    </div>    
                </div>
             </div>";
            }                    
            else if($type=='U'){
                echo"<h4 class='modal-title' id='myModalLabel'>Update template</h4>
                    </div>
                    <div class='modal-body'>";
                updatetemplate($templateID);
                echo"</div>   
                    <div class='modal-footer'>                   
                    <button id='tpl_update' type='button' class='btn btn-primary' data-dismiss='modal' data-id='$templateID'>Update</button>
                    </div>    
                </div>
             </div>";
            }
            else if($type=='D'){
                echo"<h4 class='modal-title' id='myModalLabel'>Delete Template</h4>
                    </div>
                    ";
                deletetemplateshow($templateID);
            }
    }
    function deletetemplateshow($templateID){
        include"sqlconnect.php";
        $sql="SELECT count(1) as num FROM task WHERE templateID=$templateID";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            echo"<div class='modal-body'>
                    <lable>This template is already in use</lable>
                </div>   
                    <div class='modal-footer'>                   
                    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                    </div>    
                </div>
             </div>";
        }
        else{
            echo"<div class='modal-body'>
                    <lable>This template is not in use.Are you sure to delete this template? The deleted template can't be recovered</lable>
                </div>   
                    <div class='modal-footer'>                   
                    <button id='delete_tpl' type='button' class='btn btn-danger' data-dismiss='modal' data-id='$templateID'>DELETE</button>
                    </div>    
                </div>
             </div>";
        }
    }
    function untSelect($unitType){
        include'sqlconnect.php';
        $sql="SELECT *FROM unittype";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $type=$row['UnitID'];
            $typename=$row['TypeName'];
            if($type==$unitType){
                echo"<option value='$type' selected='selected'>$typename</option>";
            }
            else{
                echo"<option value='$type'>$typename</option>";
            }
        }
    }
    function updatetemplate($templateID){
        include 'sqlconnect.php';
        $sql="SELECT *FROM template WHERE templateID='$templateID'";
        $result=mysqli_query($conn,$sql);
        echo"<form>
                <div class='table-responsive'>
                    <table class='table table-striped home-task-table'>
                        <tbody>";
        while($row=mysqli_fetch_array($result)){
            $templateName=$row['templateName'];
            $description=$row['description'];
            $nomalWLUs=$row['normalWLUs'];
            $unitType=$row['unitType'];
            echo"<tr><td class='td-label'>Template Name:</td><td><input type='text' class='form-control' id='tpl_name' value='$templateName'></input></td></tr>
                 <tr><td class='td-label'>description:</td><td><textarea  class='form-control' row='3' id='tpl_dsc' >$description</textarea></td></tr>";
            if(mysqli_fetch_array(mysqli_query($conn,"SELECT *FROM template_hierarchy WHERE templateID='$templateID'"))){
                echo"</tbody>
                </table>
                </div>
                </form>";
            }
            else{
                echo"<tr><td class='td-label'>nomalWLUs:</td><td><input type='number' class='form-control' id='tpl_wlus' value='$nomalWLUs'></input></td></tr>
                     <tr><td class='td-label'>Type:</td><td><select class='form-control' id='unittype'>";
                     untSelect($unitType);
                echo"</td></tr>
                    </tbody>
                  </table>
                </div>
                </form>";
            }
        }
    }
    function templateview($templateID){
        include 'sqlconnect.php';
        $sql="SELECT *FROM template WHERE templateID='$templateID'";
        $result=mysqli_query($conn,$sql);
        echo"<div class='table-responsive'>
                    <table class='table table-striped home-task-table'>
                        <tbody>";
        while($row=mysqli_fetch_array($result)){
            $templateName=$row['templateName'];
            $description=$row['description'];
            $nomalWLUs=$row['normalWLUs'];
            $unitType=$row['unitType'];
        }
        echo"<tr><td class='td-label'>Template Name:</td><td>$templateName</td></tr>
             <tr><td class='td-label'>Nomal Workload:</td><td>$nomalWLUs</td></tr>";
        if($unitType==null){
            echo"<tr><td class='td-label'>Type:</td><td>$unitType</td></tr>
                 <tr><td class='td-label'>Description:</td><td>$description</td></tr>";
        }
        else{
            $sql="SELECT *FROM unittype WHERE UnitID=$unitType";
            $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){
                $typename=$row['TypeName'];
            }
            echo"<tr><td class='td-label'>Type:</td><td>$typename</td></tr>
                 <tr><td class='td-label'>Description:</td><td>$description</td></tr>";
        }
        echo"</tbody>
            </table>
            </div>
            </form>";
    }
    function showsuperlist(){
        include'sqlconnect.php';
        $sql="SELECT template.templateID,templateName FROM template,template_hierarchy 
        WHERE template.templateID not in (SELECT subtemplateID FROM template_hierarchy) group by template.templateID";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $templateID=$row['templateID'];
            $templateName=$row['templateName'];
            $levelNum=1;
            $listID="l_".$templateID;
        echo"<div class='container template-list-item template-level-$levelNum $listID' data-css='$levelNum'>
                <div class='col-md-5'>$templateName</div>       
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='V'>View</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='U'>Update</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='D'>Delete</button></div>";
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM template_hierarchy WHERE templateID='.$templateID);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
                
            }
            if($num>0){
            echo"<div class='col-md-1 ' data-id='$templateID'><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$templateID'></span></div>
                </div><div class='$templateID' style='display:none;'>";
                    showsublist($templateID,$levelNum);
            echo"</div>";
            
            }
            else{
                echo"</div>";
            }        }
    }
    function showsublist($templateID,$levelNum){
        include'sqlconnect.php';
        $sql="SELECT template.templateID,templateName FROM template,template_hierarchy
        WHERE template.templateID=template_hierarchy.subtemplateID and template_hierarchy.templateID=$templateID";
        $result=mysqli_query($conn,$sql);
        $levelNum++;
        while($row=mysqli_fetch_array($result)){
            $templateID=$row['templateID'];
            $templateName=$row['templateName'];
            $listID="l_".$templateID;  
            echo"<div class='container template-list-item template-level-$levelNum $listID' data-css='$levelNum'>
                <div class='col-md-5'>$templateName</div>       
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='V'>View</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='U'>Update</button></div>
                <div class='col-md-2'><button class='btn btn-default btn-sm' data-template='$templateID' data-id='D'>Delete</button></div>";
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM template_hierarchy WHERE templateID='.$templateID);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
            echo"<div class='col-md-1 ' ><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$templateID'></span></div>
                </div>
                <div class='$templateID' style='display:none;'>";
                    showsublist($templateID,$levelNum);
            echo"</div>";
            
            }
            else{
                echo"</div>";
            }
        }
    }
    
?>